import { FileLoader, Group, Vector3, Line, BufferGeometry, LineDashedMaterial, LineBasicMaterial } from 'three'
// import { CLonLatTrans } from '@/views/PerceivedEffect/js/projection.js'

// 颜色映射对应材质参数
const colorMap = {
  WHITE: {
    color: 0x999999,
    transparent: true,
    opacity: 1,
  },
  YELLOW: {
    color: 0x756044,
    transparent: true,
    opacity: 1,
  },
  'WHITE YELLOW': {
    color: 0x999999,
    transparent: true,
    opacity: 1,
  },
}

// 加载只有线段的geojson
export const addGeoJson = (scene) => {
  const loader = new FileLoader()
  // 经纬度转换成三维坐标 z轴为0
  // const cLonLatTrans = new CLonLatTrans(120.627035637, 31.424289987, 7.787)
  const cLonLatTrans = new CLonLatTrans(116.5597733, 39.7810684, 326)

  loader.load('/file-geo/geo.json', function (data) {
    const jsonData = JSON.parse(data)
    const geoJsonGroup = new Group()
    geoJsonGroup.name = 'geoJsonGroup'

    const features = jsonData.features
    const typeArr = []
    // 遍历数据 如果是
    features.forEach((feature) => {
      // 如果是折线 则加载折线
      if (feature.geometry.type === 'LineString') {
        // 点位坐标 线条实线还是虚线 颜色
        const coordinates = feature.geometry.coordinates
        const lineType = feature.properties.lineType
        const lineColor = feature.properties.lineColor
        const points = []

        // 解析坐标
        coordinates.forEach((coord) => {
          const [x, z] = cLonLatTrans.lonlat2xy([coord[0], coord[1]])
          // 假设雷达偏移角度是 45°
          const pos = rotateAroundZ(new Vector3(x, z, 0), 34)
          // points.push(new Vector3(x, z, 0))
          points.push(pos)
        })

        // 查看有多少种类型
        // typeArr.push(lineColor)

        switch (lineType) {
          case 'SOLID':
            addSolidLine(geoJsonGroup, points, lineColor)
            break
          case 'BROKEN':
            addBrokenLine(geoJsonGroup, points, lineColor)
            break
          case 'DOUBLE SOLID':
            addSolidLine(geoJsonGroup, points, lineColor)
            break
          case 'DOUBLE BROKEN':
            addBrokenLine(geoJsonGroup, points, lineColor)
            break
          case 'SOLID BROKEN':
            addSolidLine(geoJsonGroup, points, lineColor)
            break

          default:
            addDefauleLine(geoJsonGroup, points)
            break
        }
      }
    })

    scene.add(geoJsonGroup)
    // console.log([...new Set(typeArr)])
  })
}

// 通过原始面数据转成点数据进行渲染
export const addGeoJsonPoly = (scene, cLonLatTrans) => {
  const loader = new FileLoader()
  // file-geo
  loader.load(`/file-geo/geo.json?v=${Date.now()}`, function (data) {
    const jsonData = JSON.parse(data)
    const geoJsonGroup = new Group()
    geoJsonGroup.name = 'geoJsonGroup'

    const features = jsonData.features
    // 遍历数据 如果是
    features.forEach((feature) => {
      if (feature.geometry.type == 'Polygon') {
        const coordinates = feature.geometry.coordinates
        const points = []
        // 解析坐标
        coordinates.forEach((polygonArr) => {
          // 聚合顶点
          for (let i = 0; i < polygonArr.length; i++) {
            const polygon = polygonArr[i]
            const [x, y] = cLonLatTrans.lonlat2xy([polygon[0], polygon[1]])
            points.push(new Vector3(x, y, 0))
          }
          addBrokenLine(geoJsonGroup, points, 'WHITE')
        })
      }
    })
    scene.add(geoJsonGroup)
  })
}

// 添加折线 实线
const addSolidLine = (group, points, lineColor) => {
  const geometry = new BufferGeometry().setFromPoints(points)
  // 解析颜色
  const material = new LineBasicMaterial(colorMap[lineColor])
  // 创建 Line 对象
  const line = new Line(geometry, material)
  group.add(line)
}

const addDefauleLine = (group, points) => {
  const geometry = new BufferGeometry().setFromPoints(points)
  // 解析颜色
  const material = new LineBasicMaterial(colorMap['WHITE'])
  // 创建 Line 对象
  const line = new Line(geometry, material)
  group.add(line)
}

// 添加折线 虚线
const addBrokenLine = (group, points, lineColor) => {
  const geometry = new BufferGeometry().setFromPoints(points)
  // 解析颜色
  const material = new LineDashedMaterial(colorMap[lineColor])
  // material.dashSize = 0.1
  // material.gapSize = 0.1
  // 创建 Line 对象
  const line = new Line(geometry, material)
  line.computeLineDistances()
  group.add(line)
}

// 雷达偏移角度
const rotateAroundZ = (vector, angle) => {
  const rad = (angle * Math.PI) / 180 // 角度转弧度
  const x = vector.x * Math.cos(rad) - vector.y * Math.sin(rad)
  const y = vector.x * Math.sin(rad) + vector.y * Math.cos(rad)
  return new Vector3(x, y, vector.z)
}
